import json
import demjson
import pandas as pd
import requests
from datetime import datetime
from tqdm import tqdm

def get_org_top_board(start_date:str,end_date:str) -> pd.DataFrame:
    #url = "https://data.eastmoney.com/DataCenter_V3/stock2016/DailyStockListStatistics/pageSize=50,page=1,sortRule=-1,sortType=PBuy,startDate="\
    #  + start_date +",endDate=" + end_date +",gpfw=0,js=var%20data_tab_2.html,rt=27022254"
    #url = "https://datacenter-web.eastmoney.com/api/data/v1/get?callback=jQuery112307819288799768502_1656405469662&sortColumns=SECURITY_CODE%2CTRADE_DATE&sortTypes=1%2C-1&pageSize=50&pageNumber=1&reportName=RPT_DAILYBILLBOARD_DETAILS&columns=SECURITY_CODE%2CSECUCODE%2CSECURITY_NAME_ABBR%2CTRADE_DATE%2CEXPLAIN%2CCLOSE_PRICE%2CCHANGE_RATE%2CBILLBOARD_NET_AMT%2CBILLBOARD_BUY_AMT%2CBILLBOARD_SELL_AMT%2CBILLBOARD_DEAL_AMT%2CACCUM_AMOUNT%2CDEAL_NET_RATIO%2CDEAL_AMOUNT_RATIO%2CTURNOVERRATE%2CFREE_MARKET_CAP%2CEXPLANATION%2CD1_CLOSE_ADJCHRATE%2CD2_CLOSE_ADJCHRATE%2CD5_CLOSE_ADJCHRATE%2CD10_CLOSE_ADJCHRATE&source=WEB&client=WEB&filter=(TRADE_DATE%3C%3D%272022-06-27%27)(TRADE_DATE%3E%3D%272022-06-27%27)"
    #https://datacenter-web.eastmoney.com/api/data/v1/get?callback=jQuery11230668456738386207_1656427450298&sortColumns=NET_BUY_AMT%2CTRADE_DATE%2CSECURITY_CODE&sortTypes=-1%2C-1%2C1&pageSize=50&pageNumber=1&reportName=RPT_ORGANIZATION_TRADE_DETAILS&columns=ALL&source=WEB&client=WEB&filter=(TRADE_DATE%3E%3D%272022-06-28%27)
    url = "https://datacenter-web.eastmoney.com/api/data/v1/get"
    filter="(TRADE_DATE>='" + start_date + "')(TRADE_DATE<='"+end_date + "')"
    params = {
        #"callback": "jQuery112306723101674232936_165323781643",
        "sortColumns": "NET_BUY_AMT,TRADE_DATE,SECURITY_CODE",
        "sortTypes":"-1,-1,1",
        "pageSize": "10000",
        "pageNumber": "1",
        "reportName": "RPT_ORGANIZATION_TRADE_DETAILS",
        "columns": "SECURITY_CODE,SECURITY_NAME_ABBR,TRADE_DATE,CLOSE_PRICE,CHANGE_RATE,BUY_TIMES,SELL_TIMES,BUY_AMT,SELL_AMT,NET_BUY_AMT,D1_CLOSE_ADJCHRATE,ACCUM_AMOUNT,RATIO,TURNOVERRATE,FREECAP,EXPLANATION",
        #D1_CLOSE_ADJCHRATE,D2_CLOSE_ADJCHRATE,D3_CLOSE_ADJCHRATE,D5_CLOSE_ADJCHRATE,D10_CLOSE_ADJCHRATE,MARKET,TRADE_MARKET_CODE
        #"columns": "ALL",
        "filter": filter
    }

    r = requests.get(url, params=params)
    #data_text = r.text
    #data_json = demjson.decode(data_text[data_text.find("{"):])
    #data_json = demjson.decode(data_text)
    data_json = r.json()
    page_num = data_json['result']['pages']
    data_df = pd.DataFrame(data_json["result"]["data"])
    for page in tqdm(range(2, page_num + 1)):
        params.update({'pageNumber': page})
        r = requests.get(url, params=params)
        data_json = r.json()
        temp_df = pd.DataFrame(data_json['result']["data"])
        data_df = data_df.append(temp_df, ignore_index=True)

    data_df["TRADE_DATE"] = pd.to_datetime(data_df["TRADE_DATE"])

    if(data_df.empty == True):
        return None
    return data_df
    #return data_df[["SCode","SName","TDate","CPrice","Chgradio","BSL","SSL","BMoney","SMoney","PBuy","Ntransac","TurNover","PBRate","TurnRate","AGSZBHXS","CTypeDes"]]
   
def get_top_board(start_date:str,end_date:str) -> pd.DataFrame:
    #url = "https://data.eastmoney.com/DataCenter_V3/stock2016/TradeDetail/pagesize=200,page=1,sortRule=-1,sortType=PBuy,startDate="\
    #  + start_date +",endDate=" + end_date +",gpfw=0,js=var%20data_tab_1.html,rt=27022263"
    #url = "https://datacenter-web.eastmoney.com/api/data/v1/get?callback=jQuery112307819288799768502_1656405469662&sortColumns=SECURITY_CODE%2CTRADE_DATE&sortTypes=1%2C-1&pageSize=50&pageNumber=1&reportName=RPT_DAILYBILLBOARD_DETAILS&columns=SECURITY_CODE%2CSECUCODE%2CSECURITY_NAME_ABBR%2CTRADE_DATE%2CEXPLAIN%2CCLOSE_PRICE%2CCHANGE_RATE%2CBILLBOARD_NET_AMT%2CBILLBOARD_BUY_AMT%2CBILLBOARD_SELL_AMT%2CBILLBOARD_DEAL_AMT%2CACCUM_AMOUNT%2CDEAL_NET_RATIO%2CDEAL_AMOUNT_RATIO%2CTURNOVERRATE%2CFREE_MARKET_CAP%2CEXPLANATION%2CD1_CLOSE_ADJCHRATE%2CD2_CLOSE_ADJCHRATE%2CD5_CLOSE_ADJCHRATE%2CD10_CLOSE_ADJCHRATE&source=WEB&client=WEB&filter=(TRADE_DATE%3C%3D%272022-06-27%27)(TRADE_DATE%3E%3D%272022-06-27%27)"
    #https://datacenter-web.eastmoney.com/api/data/v1/get?callback=jQuery112307819288799768502_1656405469662&sortColumns=SECURITY_CODE,TRADE_DATE&sortTypes=1,-1&pageSize=50&pageNumber=1&reportName=RPT_DAILYBILLBOARD_DETAILS&columns=SECURITY_CODE,SECUCODE,SECURITY_NAME_ABBR,TRADE_DATE,EXPLAIN,CLOSE_PRICE,CHANGE_RATE,BILLBOARD_NET_AMT,BILLBOARD_BUY_AMT,BILLBOARD_SELL_AMT,BILLBOARD_DEAL_AMT,ACCUM_AMOUNT,DEAL_NET_RATIO,DEAL_AMOUNT_RATIO,TURNOVERRATE,FREE_MARKET_CAP,EXPLANATION,D1_CLOSE_ADJCHRATE,D2_CLOSE_ADJCHRATE,D5_CLOSE_ADJCHRATE,D10_CLOSE_ADJCHRATE&source=WEB&client=WEB&filter=(TRADE_DATE>='2022-06-27')(TRADE_DATE<='2022-06-27')
    url = "https://datacenter-web.eastmoney.com/api/data/v1/get"
    filter="(TRADE_DATE>='" + start_date + "')(TRADE_DATE<='"+end_date + "')"
    params = {
        #"callback": "jQuery112306723101674232936_165323781643",
        "sortColumns": "SECURITY_CODE,TRADE_DATE",
        "sortTypes":"1,-1",
        "pageSize": "10000",
        "pageNumber": "1",
        "reportName": "RPT_DAILYBILLBOARD_DETAILS",
        "columns": "SECURITY_CODE,SECURITY_NAME_ABBR,TRADE_DATE,CLOSE_PRICE,CHANGE_RATE,BILLBOARD_BUY_AMT,BILLBOARD_SELL_AMT,BILLBOARD_NET_AMT,DEAL_AMOUNT_RATIO,BILLBOARD_DEAL_AMT,DEAL_NET_RATIO,TURNOVERRATE,FREE_MARKET_CAP,EXPLAIN,EXPLANATION",
        "filter": filter
    }

    r = requests.get(url, params=params)
 
    data_json = r.json()
    page_num = data_json['result']['pages']
    data_df = pd.DataFrame(data_json["result"]["data"])
    for page in tqdm(range(2, page_num + 1)):
        params.update({'pageNumber': page})
        r = requests.get(url, params=params)
        data_json = r.json()
        temp_df = pd.DataFrame(data_json['result']["data"])
        data_df = data_df.append(temp_df, ignore_index=True)

    data_df["TRADE_DATE"] = pd.to_datetime(data_df["TRADE_DATE"])

    if(data_df.empty == True):
        return None
    return data_df
      

if __name__ == '__main__':
    pd.set_option('display.unicode.ambiguous_as_wide', True)
    pd.set_option('display.unicode.east_asian_width', True)
    pd.set_option('display.max_rows', None)

    now = datetime.now()
    datestr = datetime.strftime(now,'%Y-%m-%d')
    df = get_org_top_board(datestr,datestr)
    if(df is not None):
      print(df)
    else:
      print("get_org_top_board no data:" + datestr)
    df = get_top_board(datestr,datestr)
    if(df is not None):
      print(df)
    else:
      print("get_top_board no data:" + datestr)